﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MarvellousWorks.PracticalPattern.Common
{
	public static class ConfigurationBroker
	{

		private static readonly GenericCache<Type, object> cache;

		static ConfigurationBroker()
		{
			System.Configuration.Configuration config
				= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
			cache = new GenericCache<Type, object>();

			foreach (ConfigurationSectionGroup group in config.SectionGroups)
			{
				if (typeof(IConfigurationSource).IsAssignableFrom(group.GetType()))
					((IConfigurationSource)group).Load();
			}

		}

		public static void Add(Type type, object item)
		{
			if ((type == null) || (item == null)) throw new NullReferenceException();
			cache.Add(type, item);
		}

		public static void Add(KeyValuePair<Type, object> item) { Add(item.Key, item.Value); }
		public static void Add(object item) { Add(item.GetType(), item); }

		public static T GetConfigurationObject<T>() where T : class
		{
			if (cache.Count <= 0) return null;
			object result;
			if (!cache.TryGetValue(typeof(T), out result))
				return null;
			else
				return (T)result;
		}
	}
}
